/*
 * Time.cpp
 *
 *	Author: stravant
 *	Description:
 *		
 */

#include "Time.h"

//constructors
TimeDelta TimeDelta::FromDays(int64 days) {
	return TimeDelta(days * Time::kMicrosecondsPerDay);
}

TimeDelta TimeDelta::FromHours(int64 hours) {
	return TimeDelta(hours * Time::kMicrosecondsPerHour);
}

TimeDelta TimeDelta::FromMinutes(int64 minutes) {
	return TimeDelta(minutes * Time::kMicrosecondsPerMinute);
}

TimeDelta TimeDelta::FromSeconds(int64 seconds) {
	return TimeDelta(seconds * Time::kMicrosecondsPerSecond);
}

TimeDelta TimeDelta::FromMilliseconds(int64 millis) {
	return TimeDelta(millis * Time::kMicrosecondsPerMillisecond);
}

TimeDelta TimeDelta::FromMicroseconds(int64 us) {
	return TimeDelta(us);
}

//inXX
int TimeDelta::inDays() const {
	return static_cast<int>(mDelta / Time::kMicrosecondsPerDay);
}

int TimeDelta::inHours() const {
	return static_cast<int>(mDelta / Time::kMicrosecondsPerHour);
}

int TimeDelta::inMinutes() const {
	return static_cast<int>(mDelta / Time::kMicrosecondsPerMinute);
}

int64 TimeDelta::inSeconds() const {
	return static_cast<int64>(mDelta / Time::kMicrosecondsPerSecond);
}

int64 TimeDelta::inMilliseconds() const {
	return static_cast<int64>(mDelta / Time::kMicrosecondsPerMillisecond);
}

int64 TimeDelta::inMicroseconds() const {
	return mDelta;
}

//#include <iostream>
double TimeDelta::inSecondsF() const {
	double sec = static_cast<double>(mDelta) / static_cast<double>(Time::kMicrosecondsPerSecond);
	//std::cout << "inSeconds: " << mDelta << "us => " << sec << "s\n";
	return sec;
}

//////////////////////////////////////////////////


#if defined(OS_WIN)
const int64 Time::kMicrosecondOffset = 11644473600 * Time::kMicrosecondsPerSecond;
#elif defined(OS_POSIX)
const int64 Time::kMicrosecondOffset = 11644473600;
#endif

Time Time::UnixEpoch() {
	Time time;
	time.mUs = kMicrosecondOffset;
	return time;
}

Time Time::Now() {
#if defined(OS_WIN)

#error TODO Implement Windows Time::Now()

#elif defined(OS_POSIX)
	struct timeval tv;
	struct timezone tz = {0, 0};
	if (gettimeofday(&tv, &tz) != 0) {
		unreachable;
	}
	return Time((tv.tv_sec * kMicrosecondsPerSecond) + tv.tv_usec + kMicrosecondOffset);
#endif
}

























